package com.circle.rest.modular.nt.controller;

import com.circle.rest.core.aop.Log;
import com.circle.rest.core.aop.Permission;
import com.circle.rest.core.common.enums.BusinessType;
import com.circle.rest.core.common.model.SuccessResponseData;
import com.circle.rest.core.common.page.PageInfoBT;
import com.circle.rest.core.exception.ServiceException;
import com.circle.rest.core.util.ToolUtil;
import com.circle.rest.modular.nt.model.Content;
import com.circle.rest.modular.nt.wrapper.ContentWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import com.circle.rest.modular.nt.service.IContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import java.time.LocalDateTime;

/**
 * <p>
 * 通知信息 前端控制器
 * </p>
 *
 * @author 赵航
 * @since 2019-04-09
 */
@Api(value = "通知信息", tags = {"nt : 通知信息API"})
@RestController
@RequestMapping("/nt/content")
public class ContentController {

    @Autowired
    private IContentService contentService;

    /**
     * 查询通知信息列表
     */
    @ApiOperation("查询通知信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "起始数据下标", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "数据条数", dataType = "int"),
            @ApiImplicitParam(name = "condition", value = "关键字", dataType = "String")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @Log(title = "查询通知信息列表", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object list(Integer index, Integer size, String condition) {
        Page<Content> page = new Page<>(index == null ? 1 : index, size == null ? 15 : size);
        QueryWrapper<Content> wrapper = new QueryWrapper<>();
        IPage<Map<String, Object>> mapIPage = contentService.pageMaps(page, wrapper);
        PageInfoBT<Object> objectPageInfoBT = new PageInfoBT<>(new ContentWrapper(mapIPage).wrap());
        return new SuccessResponseData(objectPageInfoBT);
    }

    /**
     * 添加通知信息
     */
    @ApiOperation("添加通知信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Log(title = "添加通知信息", businessType = BusinessType.INSERT)
    @Permission
    public Object add(Content content) {
        content.setUpdateTime(LocalDateTime.now());
        content.setCreateTime(LocalDateTime.now());
        boolean save = contentService.save(content);
        return new SuccessResponseData(save);
    }

    /**
     * 修改通知信息
     */
    @ApiOperation("修改通知信息")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @Log(title = "修改通知信息", businessType = BusinessType.UPDATE)
    @Permission
    public Object edit(Content content) {
        if (ToolUtil.isEmpty(content.getId())) {
            throw new ServiceException("编号不能为空");
        }
        if (ToolUtil.isEmpty(content.getVersion())) {
            throw new ServiceException("版本号不能为空");
        }
        content.setUpdateTime(LocalDateTime.now());
        if (!contentService.updateById(content)) {
            throw new ServiceException("更新失败(版本号不一致)");
        }
        return new SuccessResponseData();
    }

    /**
     * 删除通知信息
     */
    @ApiOperation("删除通知信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "通知信息编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @Log(title = "删除通知信息", businessType = BusinessType.DELETE)
    @Permission
    public Object delete(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("通知信息编号不能为空");
        }
        Content contentOld = contentService.getById(id);
        if (ToolUtil.isEmpty(contentOld)) {
            throw new ServiceException("通知信息不存在，删除失败");
        }
        if (!contentService.removeById(id)) {
            throw new ServiceException("删除失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 查询通知信息
     */
    @ApiOperation("查询通知信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "通知信息编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @Log(title = "查询通知信息", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object detail(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("通知信息编号不能为空");
        }
        Content content = contentService.getById(id);
        if (ToolUtil.isEmpty(content)) {
            throw new ServiceException("通知信息不存在");
        }
        return new SuccessResponseData(content);
    }
}
